package com.sasu.university_data.service.impl;


import com.sasu.university_data.entity.Info;
import com.sasu.university_data.mapper.InfoMapper;
import com.sasu.university_data.service.InfoService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

@Service("infoService")
public class InfoServiceImpl implements InfoService {
    @Resource
    private InfoMapper infoMapper;

    /**
     * 通过地址查大学
     */
    @Override
    public List<Info> queryByAddress( String address) {
        String address1 = "北京,天津,上海,重庆"; //直辖市
        String address2 = "内蒙古,广西,西藏,宁夏,新疆"; //自治区
        if(address1.contains(address)){
            address += "市";
        }else if(address2.contains(address)){
            address = address + "%自治区";
        }else{
            address += "省";
        }
        return infoMapper.queryByAddress("%"+address+"%");
    }

    @Override
    public List<Info> queryByName(String name) {
        return infoMapper.queryByName(name);
    }

    @Override
    public Info queryById ( Integer id ) {
        return infoMapper.queryById(id);
    }

    @Override
    public long count ( String name ) {
        return infoMapper.count(name);
    }

    @Override
    public List<Info> selectByPage ( String name, Integer page, Integer pageSize ) {
        if(page<=1) {
            page = 1;
        }
        if(pageSize<=0) {
            pageSize = 10;
        }
        return infoMapper.selectByPage(name,(page - 1) * pageSize, pageSize);
    }

    @Override
    public boolean insert ( Info info ) {
        return infoMapper.insert(info)>0;
    }

    @Override
    public boolean update ( Info info ) {
        return infoMapper.update(info)>0;
    }

    @Override
    public boolean deleteById ( Integer id ) {
        return infoMapper.deleteById(id)>0;
    }

    @Override
    public boolean deleteByIds ( int[] ids ) {
        return infoMapper.deleteByIds(ids)>0;
    }

}
